#include "master/master.h"
#include "control/user_control.h"
#include "our_header.h"
#include "tools/my_tlv.h"
#include <unistd.h>

static int sig_pipe[2];

// 注册监听信号SINGUSR1，和触发退出逻辑
void quitForkProcess();

void sigUSerHander(int signum);

int startWork(int listen_fd, int threads_sum);

int startMaster(char *ip_addr, char *poort, int threads_sum) {
	pipe(sig_pipe);
	if(0 == fork()) {
		close(sig_pipe[1]);
		int listen_fd = Tcpinit(ip_addr, poort);
		ERROR_CHECK(listen_fd, -1, "Tcpinit error");
		startWork(listen_fd, threads_sum);
		close(listen_fd);
		printf("master exit\n");
		exit(0);
	} else {
		quitForkProcess();
	}
	return 0;
}

void quitForkProcess() {
	close(sig_pipe[0]);
	signal(SIGUSR1, sigUSerHander);
	wait(NULL);
	printf("over\n");
}

void sigUSerHander(int signum) {
	printf("recv SIGUSR1,starting kill Master\n");
	write(sig_pipe[1], "i", 2);
}

int startWork(int listen_fd, int threads_sum) {
	initMysql_7th();
	MYSQL *master_connect;
	getConnect_7th(&master_connect);

	// 初始化线程池，和mysql连接池
	ERROR_CHECK(initThreadsPoll_h(threads_sum, QUEUN_CAIPICTY), -1, "initThreadsPoll_h error--->");
	startThreadsPoll_h(threads_sum, workerTask);

	int epfd = epoll_create(1);
	epollAddFd_In(epfd, listen_fd);
	epollAddFd_In(epfd, sig_pipe[0]);
	struct epoll_event read_evnet;
	int net_id[1024] = {0};

	// clientQueue_t clientQueue;
	// init_clientQueue(&clientQueue);
	while(1) {
		bzero(&read_evnet, sizeof(read_evnet));
		int readyNum = epoll_wait(epfd, &read_evnet, 1, 100);
		// if(readyNum == 0) {
		// 	//超时处理
		// 	if(clientQueue.time_out[clientQueue.timer].size != 0) {				//当前时间槽中还有连接
		// 		slotNode_t *Cur = clientQueue.time_out[clientQueue.timer].head; //拿到超时的头节点
		// 		slotNode_t *tmp = Cur;
		// 		int netfd;
		// 		while(Cur != NULL) { //遍历处理超时连接
		// 			netfd = Cur->netfd;
		// 			tmp = Cur->next;
		// 			fdDel(netfd, &clientQueue); //把netfd从时间轮盘中删除
		// 			clientQueue.client[netfd] = -1;
		// 			epollDel(epfd, netfd);
		// 			close(netfd);
		// 			printf("超时断开连接 netfd = %d\n", netfd);
		// 			// send(netfd,"1",1,MSG_NOSIGNAL);//通知客户端
		// 			Cur = tmp;
		// 		}
		// 	}
		// 	clientQueue.timer = (clientQueue.timer + 1) % TIME_SLICE; //移动到下一个时间槽
		// }
		//超时处理 }

		//新连接
		if(read_evnet.data.fd == listen_fd) {
			int fd = accept(listen_fd, NULL, NULL);
			// SQItem item = {0};
			// item.netfd = fd;
			// printf("recv connecting %d\n", fd);
			// int tag = produceSocketFd_h(&item);
			// ERROR_CHECK(tag, -1, "produceSocket error");
			epollAddFd_In(epfd, fd);
			net_id[fd] = 1;
			// fdAdd(fd, &clientQueue);
		}

		// 消息
		if(read_evnet.data.fd != listen_fd && read_evnet.data.fd != sig_pipe[0] && net_id[read_evnet.data.fd]) {

			train_t tdata = {0};
			if(recv_train(read_evnet.data.fd, &tdata)) {

				int netfd = net_id[read_evnet.data.fd];
				// fdDel(netfd, &clientQueue);
				// 退出处理
				net_id[read_evnet.data.fd] = 0;
				close(read_evnet.data.fd);
				epollDel(epfd, read_evnet.data.fd);
			}

			switch(tdata.cmdstat) {
			case LOGIN: {
				loginControl(read_evnet.data.fd, master_connect, tdata.data);
			} break;
			case RIGIST: {
				regisControl(read_evnet.data.fd, master_connect, tdata.data);
			} break;
			case MKDIR: {
				if(mkdir_control(read_evnet.data.fd, master_connect, &tdata) == -1) {
					epollDel(epfd, read_evnet.data.fd);
					// fdDel(read_evnet.data.fd, &clientQueue);
					close(read_evnet.data.fd);
				};
			} break;
			case RMDIR: {
				if(rmdir_control(read_evnet.data.fd, master_connect, &tdata) == -1) {
					epollDel(epfd, read_evnet.data.fd);
					// fdDel(read_evnet.data.fd, &clientQueue);
					close(read_evnet.data.fd);
				};
			} break;
			case LS: {
				printf("lls\n");
				if(lsControl(read_evnet.data.fd, master_connect, &tdata) == -1) {
					epollDel(epfd, read_evnet.data.fd);
					// fdDel(read_evnet.data.fd, &clientQueue);
					close(read_evnet.data.fd);
				};
			}; break;
			case CD: {
				printf("CD \n");
				if(dirControl(read_evnet.data.fd, master_connect, &tdata) == -1) {
					epollDel(epfd, read_evnet.data.fd);
					// fdDel(read_evnet.data.fd, &clientQueue);
					close(read_evnet.data.fd);
				};
			}; break;
			case PUTS: {
				// fdDel(read_evnet.data.fd, &clientQueue);
				epollDel(epfd, read_evnet.data.fd);
				if(longCmdTranfor(read_evnet.data.fd, master_connect, &tdata)) {
					close(read_evnet.data.fd);
				}
			} break;
			case GETS: {
				// fdDel(read_evnet.data.fd, &clientQueue);
				if(longCmdTranfor(read_evnet.data.fd, master_connect, &tdata))
					close(read_evnet.data.fd);
				epollDel(epfd, read_evnet.data.fd);
			} break;
			default: {
				printf("defualt\n");
				epollDel(epfd, read_evnet.data.fd);
				// fdDel(read_evnet.data.fd, &clientQueue);
				close(read_evnet.data.fd);
			} break;
			}
		}

		//退出触发
		if(read_evnet.data.fd == sig_pipe[0]) {
			closeConnect_7th(master_connect);
			exitThreadsPoll_h();
			break;
		}
	}

	closeMysql_7th();
	close(epfd);
	return 0;
}
